package com.estate.system.service.impl;

import java.util.List;

import com.google.gson.Gson;
import com.estate.common.utils.redis.RedisUtil;
import com.estate.system.domain.Apartment;
import com.estate.system.domain.ApartmentDetailAndApartment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.estate.system.mapper.ApartmentDetailMapper;
import com.estate.system.domain.ApartmentDetail;
import com.estate.system.service.IApartmentDetailService;
import com.estate.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author estate
 * @date 2022-05-28
 */
@Service
public class ApartmentDetailServiceImpl implements IApartmentDetailService
{
    @Resource
    private ApartmentDetailMapper apartmentDetailMapper;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ApartmentDetail selectApartmentDetailById(Long id)
    {

        Gson gson = new Gson();
        String apartmentDetailJson = (String)redisUtil.get("apartmentDetail:" + id);
        //有可能存在缓存穿透的问题
        ApartmentDetail apartmentDetail = new ApartmentDetail();
        if (apartmentDetailJson!=null && apartmentDetailJson.equals("noExist")){
            System.out.println("bothnoExist1");
            return null;
        }else if(apartmentDetailJson==null){
            apartmentDetail=apartmentDetailMapper.selectApartmentDetailById(id);
            if (apartmentDetail==null){
                redisUtil.set("apartmentDetail:"+id,"noExist");
                System.out.println("bothnoExist");
                return null;
            }else{
                String s = gson.toJson(apartmentDetail);
                redisUtil.set("apartmentDetail:"+id,s);
                return apartmentDetail;
            }

        }else {
            //不等于null和noExist,还有也不是null,说明存在直接返回
            ApartmentDetail apartmentDetail1 = gson.fromJson(apartmentDetailJson, ApartmentDetail.class);

            return apartmentDetail1;

        }




    }

    /**
     * 左连接查出 apartment和detail表共同的信息
     *  用redis改造过 还未测试
     * **/
    @Override
    public ApartmentDetailAndApartment selectApartmentDetailAndApartmentList(ApartmentDetail apartmentDetail) {

        return apartmentDetailMapper.selectApartmentDetailAndApartmentList(apartmentDetail);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param apartmentDetail 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ApartmentDetail> selectApartmentDetailList(ApartmentDetail apartmentDetail)
    {
        return apartmentDetailMapper.selectApartmentDetailList(apartmentDetail);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param apartmentDetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertApartmentDetail(ApartmentDetail apartmentDetail)
    {

        int i = apartmentDetailMapper.insertApartmentDetail(apartmentDetail);
        if (i>0){
            redisUtil.del("apartmentDetail:"+apartmentDetail.getId());
            return i;
        }
        return i;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param apartmentDetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateApartmentDetail(ApartmentDetail apartmentDetail)
    {
        int i = apartmentDetailMapper.updateApartmentDetail(apartmentDetail);
        if (i>0){
            redisUtil.del("apartmentDetail:"+apartmentDetail.getId());
            return i;
        }
        return i;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteApartmentDetailByIds(String ids)
    {

        int i = apartmentDetailMapper.deleteApartmentDetailByIds(Convert.toStrArray(ids));
        if (i>0){
            String[] strings = Convert.toStrArray(ids);
            for (String key:strings){
                redisUtil.del("apartmentDetail:"+key);
            }

            return i;
        }
        return i;
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteApartmentDetailById(Long id)
    {
        int i = apartmentDetailMapper.deleteApartmentDetailById(id);
        if(i>0){
            redisUtil.del("apartmentDetail:"+id);
            return i;
        }
        return i;
    }
}
